package com.itheima.springbootmybatis.controller;

import com.itheima.springbootmybatis.pojo.Merchant;
import com.itheima.springbootmybatis.pojo.Result;
import com.itheima.springbootmybatis.service.UserService;
import com.itheima.springbootmybatis.utils.JwtUtil;
import com.itheima.springbootmybatis.utils.Md5Util;
import com.itheima.springbootmybatis.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    //用户注册
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password,
                           String nickname,String contactPhone,String contactAddress){

         //查询用户
            Merchant u= userService.findByUserName(username);
            if (u==null){
                //没有占用//注册
                userService.register(username,password,nickname,contactPhone,contactAddress);
                return Result.success();
            }else {
                //被占用了
                return Result.error("用户名已存在，请重新输入");

            }
    }

     //登录认证
    @PostMapping("/login")
    public Result<String>login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password){
        //根据用户名查询用户
        Merchant loginUser=userService.findByUserName(username);
        //判断改用户是否存在
        if (loginUser==null){return Result.error("用户名错误");}
        //判断密码是否正确loginUser中的password是密文的
        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())){
            //登录成功
            Map<String,Object>claims=new HashMap<>();
            claims.put("id",loginUser.getId());
            claims.put("username",loginUser.getUsername());
           String token= JwtUtil.genToken(claims);
           //把token存储到redis中
            //往redis中存储一个键值对，StringRedisTemplate
            ValueOperations<String,String> operations=stringRedisTemplate.opsForValue();
            operations.set(token,token,12, TimeUnit.HOURS);


            return Result.success(token);
        }
        return Result.error("密码错误");
    }

//商户信息详情
   @GetMapping("/userInfo")
    public Result<Merchant>userInfo(@RequestHeader(name = "Authorization") String token){
        //根据用户名查询用户
      Map<String,Object>map=JwtUtil.parseToken(token);
        String usename=(String) map.get("username");
       map = ThreadLocalUtil.get();
       String username=(String)map.get("username");
        Merchant user=userService.findByUserName(username);
        return Result.success(user);
    }

}
